using Engine.Graphics;
using System;
using System.Windows.Forms;

namespace ForestBorder
{
    public partial class PropertiesForForest : UserControl
    {
        #region Private Fields

        private Scenario_Editor.AddinManager.IMaterialEditorControl _materialEditControl;
        private bool duringUpdate = false;
        private bool haspropertiesChanged = false;
        private ForestBorder.Properties properties;

        #endregion Private Fields

        #region Public Constructors

        public PropertiesForForest()
        {
            InitializeComponent();
            //  this.properties = new Properties((float)this.numericUpDown_Height.Value, (float)this.numericUpDown_Height.Value, new Material("empty", "empty", "empty", EnumMappingMode.material208));
        }

        #endregion Public Constructors

        #region Internal Events

        internal event EventHandler MaterialEditorSizeChanged;

        #endregion Internal Events

        #region Public Properties

        public Scenario_Editor.AddinManager.IAddinScenarioEditor AddinScenarioEditor { get; set; }

        public bool HaspropertiesChanged
        {
            get
            {
                bool temp = haspropertiesChanged;
                haspropertiesChanged = false;
                return temp;
            }
            set
            {
                haspropertiesChanged = value;
            }
        }

        internal ForestBorder.Properties Properties
        {
            get
            {
                return new Properties(properties);
            }
            set
            {
                properties = value;
                UpdateControl();
                this.HaspropertiesChanged = true;
            }
        }

        #endregion Public Properties

        #region Internal Methods

        internal void SetGameVertexAddin(ICreateForestBorderMesh forestBorderAddin)
        {
            this.properties.SetGameVertexData(forestBorderAddin, forestBorderAddin.GetStandardVertexData());
        }

        internal void SetMaterialEditUI(Scenario_Editor.AddinManager.IAddinScenarioEditor addin)
        {
            if (_materialEditControl != null)
            {
                //Remove old events to Control which will be removed.
                _materialEditControl.MaterialChanged -= _materialEditControl_MaterialChanged;
            }

            this._materialEditControl = addin.GetNewMaterialEditControl();

            if (this._materialEditControl is Control)
            {
                Control control = (Control)this._materialEditControl;
                this.tableLayoutPanel1.Height = control.Height + groupBox_Properties.Height + this.tableLayoutPanel1.Margin.Vertical * 2;
                this.tableLayoutPanel1.RowStyles[0].Height = control.Height;

                control.Dock = DockStyle.Fill;
                this.panel_ForAddinMaterialEditUI.Controls.Clear();
                this.panel_ForAddinMaterialEditUI.Controls.Add(control);

                // after insert materialedit-contl set new height of propertiscontrol.
                this.Size = new System.Drawing.Size(this.Size.Width, (int)this.tableLayoutPanel1.RowStyles[0].Height + this.groupBox_Properties.Height);

                if (this.MaterialEditorSizeChanged != null)
                    MaterialEditorSizeChanged(this, null);
            }

            if (this._materialEditControl is Scenario_Editor.AddinManager.IMaterialEditorControl)
            {
                _materialEditControl.MaterialChanged += _materialEditControl_MaterialChanged;
                this.properties.Material = _materialEditControl.GetNewMaterial();
            }
        }

        internal void SetTextureOffsetEnd(float textureOffset)
        {
            this.numericUpDown_TextureOffseEnd.Value = (decimal)textureOffset;
        }

        internal void SetTextureOffsetStart()
        {
            this.numericUpDown_TextureOffset.Value = this.numericUpDown_TextureOffseEnd.Value;
            haspropertiesChanged = true;
        }

        internal void UpdateTextureList(MaterialManager materialManager)
        {
        }

        #endregion Internal Methods

        #region Private Methods

        private void _materialEditControl_MaterialChanged(object sender, EventArgs e)
        {
            Properties_ValueChanged(sender, e);
        }

        private void Properties_ValueChanged(object sender, EventArgs e)
        {
            if (!duringUpdate)
            {
                this.properties.HeightStart = (float)this.numericUpDown_Height.Value;
                this.properties.HeightEnd = (float)this.numericUpDown_Height.Value;
                this.properties.TextureOffsetStarts = (float)this.numericUpDown_TextureOffset.Value;
                this.properties.HeightOffset = (float)this.numericUpDown_HeightOffset.Value;
                this.properties.TexturCoordinateScale = (float)this.numericUpDown_TextureScale.Value;
                this.properties.Material = _materialEditControl.GetNewMaterial();
                this.haspropertiesChanged = true;
            }
        }

        private void PropertiesForForest_Load(object sender, EventArgs e)
        {
            Properties_ValueChanged(null, null);
        }

        private void SetNewTextureCoordinate(object sender, EventArgs e)
        {
            this.numericUpDown_TextureOffset.Value = this.numericUpDown_TextureOffseEnd.Value;
        }

        private void UpdateControl()
        {
            this.SuspendLayout();
            this.duringUpdate = true;

            this.numericUpDown_TextureOffset.Value = (decimal)this.properties.TextureOffsetStarts;
            this.numericUpDown_Height.Value = (decimal)this.properties.HeightStart;

            this._materialEditControl.SetMaterial(this.properties.Material);

            this.numericUpDown_TextureScale.Value = (decimal)this.properties.TexturCoordinateScale;
            this.numericUpDown_HeightOffset.Value = (decimal)this.properties.HeightOffset;

            this.duringUpdate = false;
            this.ResumeLayout();
        }

        #endregion Private Methods
    }
}